package com.cyan.teamquestionnaire.controller;

import com.cyan.teamquestionnaire.beans.HttpResponseEntity;
import com.cyan.teamquestionnaire.beans.PageInfo;
import com.cyan.teamquestionnaire.entity.AnswerSheet;
import com.cyan.teamquestionnaire.entity.Questionnaire;
import com.cyan.teamquestionnaire.service.AnswerSheetService;
import com.cyan.teamquestionnaire.utils.Constants;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class AnswerSheetController {

    @Resource
    AnswerSheetService answerSheetService;

    @Resource
    HttpResponseEntity httpResponseEntity;

    /**
     * 在发布问卷时，就指定了需要回答问题的人，就会在answer_sheet表中加入相关数据，但是此时answer都是null，
     * 直到有人回答了自己的问卷，在数据库中answer才会有内容，所以后端的sql语句就是看answer是不是null来判断
     * 该问卷是不是被回答了。
     */

    // 某人需要回答的问卷集合（未分页）
    @RequestMapping(value = "/needToAnswer", method = RequestMethod.POST)
    public HttpResponseEntity needToAnswer(String name) {
        httpResponseEntity.setData(answerSheetService.questionnaireForOne(name, true));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);

        return httpResponseEntity;
    }

    // 某人已经回答了的问卷集合（未分页）
    @RequestMapping(value = "/haveAnswered", method = RequestMethod.POST)
    public HttpResponseEntity haveAnswered(String name) {
        httpResponseEntity.setData(answerSheetService.questionnaireForOne(name, false));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    // 某人需要回答的问卷集合（分页）
    @RequestMapping(value = "/needToAnswerY", method = RequestMethod.POST)
    public HttpResponseEntity needToAnswerY(@RequestBody PageInfo<Questionnaire> pageInfo) {

        httpResponseEntity.setData(answerSheetService.questionnaireForOneY(pageInfo, true));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        System.out.println(httpResponseEntity);
        return httpResponseEntity;
    }

    // 某人已回答的问卷集合（分页）
    @RequestMapping(value = "/haveAnsweredY", method = RequestMethod.POST)
    public HttpResponseEntity haveAnsweredY(@RequestBody PageInfo<Questionnaire> pageInfo) {
//        System.out.println(pageInfo);
        httpResponseEntity.setData(answerSheetService.questionnaireForOneY(pageInfo, false));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
//        System.out.println(httpResponseEntity);
        return httpResponseEntity;
    }

    /**
     * 某人需要回答的问卷集合
     * 传入name和title进行模糊查询（未分页）
     */
    @RequestMapping(value = "/needToAnswerFuzzy", method = RequestMethod.POST)
    public HttpResponseEntity needToAnswerFuzzy(@RequestBody Map<String, Object> map) {

        String name = (String) map.get("name");
        String title = (String) map.get("title");
        httpResponseEntity.setData(answerSheetService.questionnaireForOneFuzzy(name, title, true));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);

        return httpResponseEntity;
    }

    /**
     * 某人已经回答的问卷集合
     * 传入name和title进行模糊查询（未分页）
     */
    @RequestMapping(value = "/haveAnsweredFuzzy", method = RequestMethod.POST)
    public HttpResponseEntity haveAnsweredFuzzy(@RequestBody Map<String, Object> map) {
        String name = (String) map.get("name");
        String title = (String) map.get("title");

        httpResponseEntity.setData(answerSheetService.questionnaireForOneFuzzy(name, title, false));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 回答问卷
     * 传入AnswerSheet属性类
     */
    @RequestMapping(value = "/answerQuestionnaire", method = RequestMethod.POST)
    public HttpResponseEntity answerQuestionnaire(@RequestBody AnswerSheet answerSheet) {
        System.out.println(answerSheet);
        //调用回答问题
        answerSheetService.answerQuestionnaire(answerSheet);
        httpResponseEntity.setData(null);
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.Answer_Questionnaire_Over);
        return httpResponseEntity;
    }


    /**
     * 根据qId批量查询这个问卷回答情况————不分页
     * 传入qId和name，name模糊查询 ————注意传入变量名
     */
    @RequestMapping(value = "/questionnaireAnswered", method = RequestMethod.POST)
    public HttpResponseEntity questionnaireAnswered(@RequestBody Map<String,Object> map) {
        System.out.println(map);
        String qId = (String) map.get("qId");
        String name = (String) map.get("name");
        httpResponseEntity.setData(answerSheetService.questionnaireForSelf(qId,name,false));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        System.out.println(httpResponseEntity.getData());
        return httpResponseEntity;
    }

    /**
     * 根据qId批量查询这个问卷还没有回答情况————不分页
     * 传入qId和name，name模糊查询 ————注意传入变量名
     */
    @RequestMapping(value = "/questionnaireNotAnswered", method = RequestMethod.POST)
    public HttpResponseEntity questionnaireNotAnswered(@RequestBody Map<String,Object> map) {
        String qId = (String) map.get("qId");
        String name = (String) map.get("name");
        httpResponseEntity.setData(answerSheetService.questionnaireForSelf(qId,name,true));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 根据qId批量查询这个问卷回答情况————分页
     * 传入qId，query模糊查询用户名 ————注意传入变量名
     */
    @RequestMapping(value = "/questionnaireAnsweredY", method = RequestMethod.POST)
    public HttpResponseEntity questionnaireAnsweredY(@RequestBody PageInfo<AnswerSheet> pageInfo) {
        httpResponseEntity.setData(answerSheetService.questionnaireForSelfY(pageInfo,false));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 根据qId批量查询这个问卷还没有回答情况————分页
     * 传入qId，query模糊查询用户名 ————注意传入变量名
     */
    @RequestMapping(value = "/questionnaireNotAnsweredY", method = RequestMethod.POST)
    public HttpResponseEntity questionnaireNotAnsweredY(@RequestBody PageInfo<AnswerSheet> pageInfo) {
        httpResponseEntity.setData(answerSheetService.questionnaireForSelfY(pageInfo,true));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }


    /**
     * 根据QID和name查询问卷回答情况
     */
    @RequestMapping(value = "/AnswerbyQidName", method = RequestMethod.POST)
    public HttpResponseEntity AnswerbyQidName(@RequestBody AnswerSheet answerSheet) {
        System.out.println(answerSheetService.AnswerbyQidName(answerSheet));
        httpResponseEntity.setData(answerSheetService.AnswerbyQidName(answerSheet));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        System.out.println(httpResponseEntity.getData());
        return httpResponseEntity;
    }
}
